package com.ct.gd.passport.common.connect;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.json.JSONException;
import org.json.JSONObject;

import com.up91.common.android.connect.UpHttpClientHelper;
import com.up91.common.android.connect.UpHttpClientHelper.OnResponseEntityObtainedListener;
import com.up91.common.android.exception.BizException;
import com.up91.common.android.exception.NetworkException;
/**
 * 1.解析服务端返回数据, template: {"Code":0, "Data":"sampleData", "Message":"sampleMessage"}<br/>
 *
 * @author geechrist
 */
public class OnResponseEntityObtainedListenerImpl implements OnResponseEntityObtainedListener{

	private static final int	CODE_NORMAL					= 0;
	
	/**
	 * template {"Code":0, "Data":"sampleData", "Message":"sampleMessage"}
	 */
	@Override
	public String onResponseEntityObtained(String responseEntity, UpHttpClientHelper upHttpClientHelper, HttpClient httpClient, HttpUriRequest request) {
		try {
			JSONObject jObj = new JSONObject(responseEntity);
			int code = jObj.getInt("Code");
			switch (code) {
			case CODE_NORMAL:
				return jObj.getString("Data");
			default:
				throw new BizException(jObj.optString("Message"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
			throw new NetworkException();
		}
	}

}
